import React, {PureComponent} from 'react';
import {connect} from 'dva';
import {
    Form, Input, Button, Card,Radio, Select,Cascader
} from 'antd';
import {message} from "antd/lib/index";

const FormItem = Form.Item;
const RadioGroup = Radio.Group;

@connect(state => ({
    submitting: state.systemPermissionListEdit.loading,
}))
@Form.create()
export default class SystemPermissionListEdit extends PureComponent {
    state = {
        parentId: '',
        parentNode:[],
        data: ''
    }

    componentDidMount() {
        const {record} = this.props;
        const {dispatch} = this.props;
        console.log("findParentNode");
        dispatch({
            type: 'systemPermissionListEdit/findParentNode',
            payload: {action_id: record.ACTION_ID},
            callback: (response) => {
                if (response.result === 0) {
                    this.setState({
                        parentNode: response.resultData.parentList
                    });

                } else {
                    message.success('失败');
                }
            }
        });
    }

    handleSubmit = (e) => {
        const editType=this.props.editType;
        const action_id=editType==='add'?'':this.props.record.ACTION_ID;
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                var parentId = 1;
                if (values["parent_node"].length > 0) {
                    parentId = values["parent_node"][values["parent_node"].length - 1];
                }

                this.props.dispatch({
                    type: `systemPermissionListEdit/${editType}`,
                    /*payload: values,*/
                    payload: {...values, action_id: action_id, parent_action_id: parentId},
                    callback: (response) => {console.log("=====addend")
                        if (response.result === 0) {
                            message.success('提交成功');
                            this.props.handleModalSuccess(this.props.pagination);
                        } else {
                            message.error('提交失败');
                        }
                    }
                });
            }
        });
    }

    handleParentModalVisible = (e) => {
        this.props.handleModalVisible();
    }

    render() {
        const {submitting, record, editType, typeSList} = this.props;
        const {getFieldDecorator, getFieldValue} = this.props.form;
        const formInitValue = editType === 'add' ? '': record;
        const action_id = editType === 'add' ? '' : record.ACTION_ID;
        const parent_id = editType === 'add' ? '' : record.PARENT_ACTION_ID;
        let a = [];
        a.push(parent_id);
        let parent_node = editType === 'add' ? [] : eval("["+formInitValue.parent_node+"]");

        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 7},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 12},
                md: {span: 10},
            },
        };
        const submitFormLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 10, offset: 7},
            },
        };

        return (
            <Card bordered={false}>
                <Form
                    onSubmit={this.handleSubmit}
                    hideRequiredMark
                >
                    <FormItem
                        {...formItemLayout}
                        label={<span>父权限编码</span>}
                    >
                        {getFieldDecorator('parent_node', {initialValue: parent_node || []})(
                            <Cascader options={this.state.parentNode} changeOnSelect/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="系统权限编码"
                    >
                        {getFieldDecorator('ACTION_CODE', {
                            initialValue:  formInitValue.ACTION_CODE || '',
                            rules: [{
                                required: true, message: '请输入系统权限编码',
                            },{
                                max: 64, message:'最大字段长度为64',
                            }
                            ],
                        })(
                            <Input placeholder="系统权限编码"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="系统权限描述"
                    >
                        {getFieldDecorator('ACTION_DESC', {
                            initialValue:  formInitValue.ACTION_DESC || '',
                            rules: [{
                                required: true, message: '请输入系统权限描述',
                            },{
                                max: 255, message:'最大字段长度为255',
                            }
                            ],
                        })(
                            <Input placeholder="系统权限描述"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="权限类型"
                    >
                        {getFieldDecorator('action_type',{
                            initialValue:formInitValue !==''?((formInitValue.action_type?"1":"0")||''):'',
                        })
                        (<Select placeholder="请选择" style={{width: '100%'}} >
                                <Option value="">请选择</Option>
                                <Option value="0">平台权限</Option>
                                <Option value="1">商家权限</Option>
                            </Select>
                        )}
                    </FormItem>
                    <FormItem {...submitFormLayout} style={{marginTop: 32}}>
                        <Button type="primary" loading={submitting} onClick={this.handleSubmit.bind(this,editType,action_id)}>
                            提交
                        </Button>
                        <Button style={{marginLeft: 8}} onClick={this.handleParentModalVisible}>取消</Button>
                    </FormItem>
                </Form>
            </Card>
        );
    }
}
